﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using LightPersist;

namespace SmartDeviceExample
{
	public static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThread]
		public static void Main()
		{
			LightProfiling.Lap("Entry point");
#if P
			//if (!Debugger.IsAttached)
			{
				Trace.AutoFlush = true;
				var p = Environment.OSVersion.Platform==PlatformID.WinCE
				 ? "LP_rerf_log.txt"
				 : Path.GetTempFileName();
				Trace.Listeners.Add(new TextWriterTraceListener(p));
			}
			var precompiled = 0;
			P.Lap("Precompiling...");
			foreach (var asm in new[]
			                         	{
			                         		typeof(LightPersistContext).Assembly,
			                         		typeof(XmlStorageImpl).Assembly,
			                         		typeof(Program).Assembly,
			                         	})
			{
				foreach (var type in asm.GetTypes())
				{
					try
					{
						if (type.IsAbstract || type.IsInterface || type.IsEnum)
						{
							continue;
						}
						if (Activator.CreateInstance(type) == null)
						{
							throw null;
						}
						precompiled++;
					}
					catch(Exception ex)
					{
						Trace.WriteLine("Precompiling problem: "+type.Name+" "+ex.Message);
					}
				}
			}
			P.Lap("Precompiled " + precompiled);
#endif
			Debugger.Launch();
			Application.Run(new MainForm());
			Trace.Close();
		}
	}
}